#include "patientdashboard.h"
#include "appointment.h"
#include "medicalrecords.h"
#include "mainwindow.h"
#include "prescriptions.h"
#include "doctorinfo.h"
#include "editpatientinfo.h"
#include "healthassessment.h"
#include "communication.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QPalette>
#include <QFont>
#include <QDebug>

PatientDashboard::PatientDashboard(QTcpSocket *socket, MainWindow *mainWindow, QWidget *parent)
    : QWidget(parent), tcpSocket(socket), mainWindow(mainWindow) {

    setWindowTitle("患者界面");  // 设置窗口标题
    setFixedSize(600, 700);  // 设置窗口大小

    // 设置背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(220, 240, 255));
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // 设置标题
    QVBoxLayout *layout = new QVBoxLayout(this);
    QLabel *titleLabel = new QLabel("患者主界面", this);
    titleLabel->setFont(QFont("Arial", 20, QFont::Bold));
    titleLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(titleLabel);

    // 创建并设置按钮样式
    QFont buttonFont("Arial", 14);
    QString buttonStyle = "QPushButton {"
                          "background-color: #4CAF50;"
                          "color: white;"
                          "border-radius: 10px;"
                          "padding: 10px;"
                          "} "
                          "QPushButton:hover {"
                          "background-color: #45A049;"
                          "}";

    // 创建按钮
    QPushButton *appointmentButton = new QPushButton("挂号", this);
    appointmentButton->setFont(buttonFont);
    appointmentButton->setStyleSheet(buttonStyle);
    layout->addWidget(appointmentButton);

    QPushButton *medicalRecordsButton = new QPushButton("查看病例", this);
    medicalRecordsButton->setFont(buttonFont);
    medicalRecordsButton->setStyleSheet(buttonStyle);
    layout->addWidget(medicalRecordsButton);

    QPushButton *prescriptionsButton = new QPushButton("查看处方与缴费", this);
    prescriptionsButton->setFont(buttonFont);
    prescriptionsButton->setStyleSheet(buttonStyle);
    layout->addWidget(prescriptionsButton);

    QPushButton *editInfoButton = new QPushButton("编辑个人信息", this);
    editInfoButton->setFont(buttonFont);
    editInfoButton->setStyleSheet(buttonStyle);
    layout->addWidget(editInfoButton);

    QPushButton *doctorInfoButton = new QPushButton("查看医生信息", this);
    doctorInfoButton->setFont(buttonFont);
    doctorInfoButton->setStyleSheet(buttonStyle);
    layout->addWidget(doctorInfoButton);

    QPushButton *healthAssessmentButton = new QPushButton("健康评估", this);
    healthAssessmentButton->setFont(buttonFont);
    healthAssessmentButton->setStyleSheet(buttonStyle);
    layout->addWidget(healthAssessmentButton);

    QPushButton *communicationButton = new QPushButton("医患沟通平台", this);
    communicationButton->setFont(buttonFont);
    communicationButton->setStyleSheet(buttonStyle);
    layout->addWidget(communicationButton);

    QPushButton *logoutButton = new QPushButton("退出", this);
    logoutButton->setFont(buttonFont);
    logoutButton->setStyleSheet(buttonStyle);
    layout->addWidget(logoutButton);

    // 增加控件之间的间距
    layout->setContentsMargins(20, 20, 20, 20);
    layout->setSpacing(20);

    // 设置布局
    setLayout(layout);

    // 连接信号与槽
    connect(appointmentButton, &QPushButton::clicked, this, &PatientDashboard::onAppointmentButtonClicked);
    connect(medicalRecordsButton, &QPushButton::clicked, this, &PatientDashboard::onMedicalRecordsButtonClicked);
    connect(prescriptionsButton, &QPushButton::clicked, this, &PatientDashboard::onPrescriptionsButtonClicked);
    connect(editInfoButton, &QPushButton::clicked, this, &PatientDashboard::onEditInfoButtonClicked);
    connect(doctorInfoButton, &QPushButton::clicked, this, &PatientDashboard::onDoctorInfoButtonClicked);
    connect(healthAssessmentButton, &QPushButton::clicked, this, &PatientDashboard::onHealthAssessmentButtonClicked);
    connect(communicationButton, &QPushButton::clicked, this, &PatientDashboard::onCommunicationButtonClicked);
    connect(logoutButton, &QPushButton::clicked, this, &PatientDashboard::onLogoutButtonClicked);
}

void PatientDashboard::onAppointmentButtonClicked() {
    Appointment *appointment = new Appointment(tcpSocket, nullptr);
    appointment->show();
}

void PatientDashboard::onMedicalRecordsButtonClicked() {
    MedicalRecords *records = new MedicalRecords(tcpSocket, nullptr);
    records->show();
}

void PatientDashboard::onPrescriptionsButtonClicked() {
    Prescriptions *prescriptions = new Prescriptions(tcpSocket, nullptr);
    prescriptions->show();
}

void PatientDashboard::onEditInfoButtonClicked() {
    EditPatientInfo *editInfoWindow = new EditPatientInfo(tcpSocket, nullptr);
    editInfoWindow->setAttribute(Qt::WA_DeleteOnClose);
    editInfoWindow->show();
}

void PatientDashboard::onDoctorInfoButtonClicked() {
    DoctorInfo *doctorInfo = new DoctorInfo(tcpSocket, nullptr);
    doctorInfo->show();
}

void PatientDashboard::onHealthAssessmentButtonClicked() {
    HealthAssessment *assessment = new HealthAssessment(tcpSocket, nullptr);
    assessment->show();
}

void PatientDashboard::onCommunicationButtonClicked() {
    Communication *communication = new Communication(tcpSocket, nullptr);
    communication->show();
}

void PatientDashboard::onLogoutButtonClicked() {
    qDebug() << "Logout button clicked"; // 调试信息
    mainWindow->show();  // 显示登录窗口
    this->close();       // 关闭患者主界面
}
